package com.cms.recursoshumanos.ui.vacaciones;

import java.util.Calendar;
import java.util.Date;

import com.cms.recursoshumanos.cache.UserInfoCache;
import com.cms.recursoshumanos.component.base.CustomButtonColored;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.container.ForegroundManager;
import com.cms.recursoshumanos.container.TwoColumnFieldManager;
import com.cms.recursoshumanos.handler.VacacionesMainScreenHandler;
import com.cms.recursoshumanos.ui.RecursosHumanosScreen;
import com.cms.recursoshumanos.utilities.Tool;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.DateField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NumericChoiceField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class PlanillaSolicitudVacacionesScreen extends RecursosHumanosScreen
{
    /**
     * Creates a new MyScreen object
     */
	//static Utileria tools = new Utileria();
	private CustomButtonColored boton_envio_solicitud; 
	//							boton_consult_info
	NumericChoiceField ncf;
	int start = Constant.CANTIDAD_MINIMA_ARTICULO_ADD;
	int end = Constant.CANTIDAD_MAXIMA_ARTICULO;
	int increment = Constant.STEP_CANTIDAD;
	int setTo =  Constant.NCF_SET_TO;
	LabelField fecha_sol_vac_tope_text, 
				fecha_sol_vac_tope_value;
	DateField fecha_sol_vac_field;
	Date fecha_tope_sol, fecha_tope;
	HorizontalFieldManager hfm_boton;
	Border editBorder;

	VerticalFieldManager vfm_info_planilla_vacaciones;
	HorizontalFieldManager hfm_fecha_tope;
	
	public static PlanillaSolicitudVacacionesScreen instance = null;
	
	public static PlanillaSolicitudVacacionesScreen getInstance()
	{
		instance =  new PlanillaSolicitudVacacionesScreen();
		return instance;
	}
	public PlanillaSolicitudVacacionesScreen() 
	{
		super(NO_VERTICAL_SCROLL | USE_ALL_HEIGHT );
		
		Calendar c_hoy = Calendar.getInstance();
		
		c_hoy.set(Calendar.HOUR, 0);
		c_hoy.set(Calendar.MINUTE, 0);
		c_hoy.set(Calendar.SECOND, 0);

		
		long long_fecha = c_hoy.getTime().getTime();
		long_fecha = long_fecha + (24*60*60*1000);
		
		fecha_tope = new Date(long_fecha);
		fecha_tope_sol = fecha_tope; 
		
		setComponent();
	}

	public Date getFechaMinVacacion(Date fecha_tope)
	{
		Calendar c1 = Calendar.getInstance();
		c1.setTime(fecha_tope);
		
		c1.set(Calendar.HOUR, 0);
		c1.set(Calendar.MINUTE, 0);
		c1.set(Calendar.SECOND, 0);
		
		int cont = 0;
		while (cont < Constant.MIN_CANTIDAD_DIA_TOPE_VACACIONES)
		{
			long fecha = c1.getTime().getTime();
			fecha = fecha + (24*60*60*1000);
			c1.setTime(new Date (fecha));
			if (c1.get(Calendar.DAY_OF_WEEK) > 1 && // si es Domingo(1) o es Sabado(7)
					c1.get(Calendar.DAY_OF_WEEK) < 7)
			{
				cont++;
			}
		}
		
		return c1.getTime();
	}
	public Date getFechaTope(Date fecha_inic)
	{
		Calendar c1 = Calendar.getInstance();
		c1.setTime(fecha_inic);
		
		c1.set(Calendar.HOUR, 0);
		c1.set(Calendar.MINUTE, 0);
		c1.set(Calendar.SECOND, 0);
		
		int cont = 0;
		while (cont < Constant.MIN_CANTIDAD_DIA_TOPE_VACACIONES)
		{
			long fecha = c1.getTime().getTime();
			fecha = fecha - (24*60*60*1000);
			c1.setTime(new Date(fecha));;
			if (c1.get(Calendar.DAY_OF_WEEK) > 1 && // si es Domingo(1) o es Sabado(7)
					c1.get(Calendar.DAY_OF_WEEK) < 7)
			{
				cont++;
			}
		}
		
		return c1.getTime();
	}
	private void setValueComponent()
	{
		ncf = new NumericChoiceField("Dias a Sol. :", start, end, increment, 0)
		{
			
			protected boolean keyChar( char character, int status, int time ) 
		    {
		        if( character == Characters.ENTER ) 
		        {
		            return super.navigationClick(status, time);
		            //return true;
		        }
		        else
		        {
		        	if (character ==  Characters.ESCAPE)
		        	{
		        		close();
		        	}
		        }
		        return super.keyChar( character, status, time );
		    }
		    
		    protected boolean navigationClick( int status, int time ) 
		    {
		        return super.navigationClick(status, time);
		        //return true;    
		    }
		    
		    protected boolean trackwheelClick( int status, int time )
		    {        
		        return super.navigationClick(status, time);    
		        //return true;
		    }
			
		};
		
		fecha_sol_vac_tope_text = new LabelField("Fecha Tope:", FIELD_LEFT | DrawStyle.HCENTER);
		fecha_sol_vac_tope_value = new LabelField(Tool.getInstance().parseDateToString(fecha_tope, 6), USE_ALL_WIDTH | Field.FIELD_RIGHT | DrawStyle.RIGHT);
		
		fecha_sol_vac_field = new DateField("Fecha de Incio: ", getFechaMinVacacion(fecha_tope).getTime(),DateField.DATE);
		fecha_sol_vac_field.setChangeListener(new FieldChangeListener() 
		{
			
			public void fieldChanged(Field field, int context) 
			{
				Date fecha_selec = new Date(((DateField) field).getDate());
				Date fecha_tope_selec = getFechaTope(fecha_selec);
				fecha_sol_vac_tope_value.setText(Tool.getInstance().parseDateToString(fecha_tope_selec, 6));
				
				fecha_tope_sol = fecha_tope_selec;
				
			}
		});
		
		
		hfm_fecha_tope.add(fecha_sol_vac_tope_text);
		hfm_fecha_tope.add(fecha_sol_vac_tope_value);
		
		vfm_info_planilla_vacaciones.add(ncf);
		vfm_info_planilla_vacaciones.add(fecha_sol_vac_field);
		vfm_info_planilla_vacaciones.add(hfm_fecha_tope);
		
		
	}
	private void setComponent() 
	{
		
		setTitle("Vacaciones");
		
		ForegroundManager foreground = new ForegroundManager();
        TwoColumnFieldManager twoColumnManager = new TwoColumnFieldManager( Display.getWidth() / 3, USE_ALL_WIDTH | FIELD_HCENTER);
        twoColumnManager.setBorder( BorderFactory.createBitmapBorder( new XYEdges( 10, 10, 10, 10 ), Bitmap.getBitmapResource( "border_simple_white.png" ) ) );
        twoColumnManager.setMargin( 10, 10, 10, 10 );
        twoColumnManager.setPadding( 6, 6, 6, 6 );
        hfm_fecha_tope = new HorizontalFieldManager(USE_ALL_WIDTH);
		
		vfm_info_planilla_vacaciones =  new VerticalFieldManager(FIELD_HCENTER | DrawStyle.HCENTER);
		editBorder = BorderFactory.createBitmapBorder( new XYEdges( 10, 9, 9, 11 ), Bitmap.getBitmapResource( "border_edit.png" ) );
		LabelField lbl_titulo = new LabelField( "Solicitud de Planilla de Vacaciones" , 
					FIELD_HCENTER |
					FIELD_VCENTER |
					LabelField.USE_ALL_WIDTH |
					DrawStyle.HCENTER |
					DrawStyle.VCENTER);
		lbl_titulo.setFont(font_bold_8);
		LabelField lbl_info_solicitud = new LabelField("Llenar el siguiente formulario", FIELD_HCENTER | DrawStyle.HCENTER);
		
        Field separator = new SeparatorField();
        separator.setMargin( 6, 0, 6, 0 );
		
		setValueComponent();
		
        hfm_boton = new HorizontalFieldManager(Field.FIELD_HCENTER); 
        
		boton_envio_solicitud = new CustomButtonColored("Enviar Solicitud",
				ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER | DrawStyle.HCENTER, 
				Constant.COLOR_BUTTON_FOCUS); 

		boton_envio_solicitud.setChangeListener(new FieldChangeListener() 
        {
	   		public void fieldChanged(Field field, int context) 
	       	{ 
	   			Calendar c1 = Calendar.getInstance();
	   			
	   			if (c1.getTime().getTime() < fecha_tope_sol.getTime())
	   			{
	   				VacacionesMainScreenHandler handler = new VacacionesMainScreenHandler();
		   			handler.getPlanillaVacaciones(
		   											UserInfoCache.getInstance().getUsuario().getNombre_Usuario(),
		   											UserInfoCache.getInstance().getUsuario().getPassword(), 
		   											ncf.getSelectedValue(), 
		   											new Date(fecha_sol_vac_field.getDate()),
		   											fecha_tope_sol);
	   			}
	   			else
	   			{
	   				
	   			}
	   			
	   			
	   			
	       	} 
		}); 
        
       
        
        twoColumnManager.add(lbl_titulo);
        twoColumnManager.add(lbl_info_solicitud);
        twoColumnManager.add(separator);
        twoColumnManager.add(vfm_info_planilla_vacaciones);
        twoColumnManager.add(boton_envio_solicitud);
        foreground.add( twoColumnManager );
        add( foreground );
        			
	}
	
	
}
